package org.parklab.lab.web.server;


import org.parklab.lab.web.client.data.user.UserInfo;
import org.parklab.lab.web.client.rpcservice.LoginService;
import org.parklab.lab.web.server.database.UserLogon;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LoginServiceImpl extends RemoteServiceServlet implements
LoginService {
	private static final Logger LOG = Logger.getLogger(LoginServiceImpl.class.getName());
	private static final PersistenceManagerFactory PMF = JDOHelper.getPersistenceManagerFactory("transactions-optional");

	private void cw(Object msg) {
		System.out.println((new Date()).toString() + "\t" + msg.toString());
		LOG.log(Level.FINEST, msg.toString());
	}

	@Override
	public UserInfo login(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		

		if (null == user) {
			cw("null == user");
			UserInfo userInfo = new UserInfo();

			userInfo.setLoggedIn(false);
			userInfo.setLoginUrl(userService.createLoginURL(requestUri));

			cw("returning " + userInfo.toString());
			return userInfo;
		} else {
			cw("null != user");
			
			UserInfo newUser = new UserInfo();
			newUser.setUserId(user.getUserId());
			newUser.setEmailAddress(user.getEmail());
			newUser.setNickname(user.getNickname());
			newUser.setLogoutUrl(userService.createLogoutURL(requestUri));
			newUser.setLoggedIn(true);
			
			PersistenceManager pm = getPersistenceManager(); 

			List<String> symbols = new ArrayList<String>();
			try {
				Query q = pm.newQuery(UserLogon.class, "user == u");
				q.declareParameters("com.google.appengine.api.users.User u");
				q.setOrdering("lastLogin desc");
				
				List<UserLogon> userLogons = (List<UserLogon>) q.execute(user);

				cw("queried: userLogons: isEmpty(" + userLogons.isEmpty() + ")");
				if (userLogons.isEmpty()) {
					cw("because userLogons is empty:");
					
					UserLogon newLogon = new UserLogon();
					newLogon.setUser(user);
					newLogon.setLastLogin(new Date());
					pm.makePersistent(newLogon);
					
				} else {
					cw("because users isn't empty:");
					
					UserLogon aLogon = userLogons.get(0);
					
					cw("aUserLogon: " + aLogon.toString());
					
					aLogon.setLastLogin(new Date());
				}
			} catch (Exception e) {
				cw("Exception occurred: " + e.getMessage());
				LOG.severe(e.getMessage());
			} finally {
				pm.close();
			}
			
			return newUser; 
		}
	}

	private PersistenceManager getPersistenceManager() {
		return PMF.getPersistenceManager();
	}


}
